package com.example.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.seckill.exception.GlobalException;
import com.example.seckill.mapper.UserMapper;
import com.example.seckill.pojo.User;
import com.example.seckill.service.IUserService;
import com.example.seckill.utils.CookieUtil;
import com.example.seckill.utils.MD5Util;
import com.example.seckill.utils.UUIDUtil;
import com.example.seckill.vo.LoginVo;
import com.example.seckill.vo.RespBean;
import com.example.seckill.vo.RespBeanEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2022-08-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
    * @Description: 根据cookie获取用户
    * @Param:
    * @return:
    * @Author: KingCrimson
    * @Date: 2022/8/8
    */
    @Override
    public User getUserByCookie(String uuid,HttpServletRequest request,HttpServletResponse response) {
        if (StringUtils.isBlank(uuid)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + uuid);
        if (user != null){
            CookieUtil.setCookie(request,response,"userUUID",uuid);
        }
        return user;
    }

    /**
    * @Description: 登录实现类
    * @Param: 
    * @return: 
    * @Author: KingCrimson
    * @Date: 2022/8/6
    */
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //使用自定义注解代替
//        if (StringUtils.isBlank(mobile)||StringUtils.isBlank(password)){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        //根据手机号获取对象
        User user = userMapper.selectById(mobile);
        if (null == user){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //判断密码是否正确
        if (!MD5Util.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String uuid = UUIDUtil.uuid();
        //将用户信息存入redis中
        redisTemplate.opsForValue().set("user:"+uuid,user);
//        request.getSession().setAttribute(uuid,user);
        System.out.println(redisTemplate.opsForValue().get("user:"+uuid)+"is getting!");
        CookieUtil.setCookie(request,response,"userUUID",uuid);
        return RespBean.success(uuid);
    }
}
